package com.ihd.ihardware.view.tzc.me.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.RulerView;
import com.ihd.ihardware.databinding.ActivityTargetStepBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.TargetViewModel;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class TargetStepActivity extends BaseActivity<ActivityTargetStepBinding, TargetViewModel> {

    private UserInfoRes.DataBean ud;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("目标步数");
        binding.mtitlebar.setLeftBack(this);

        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);

        binding.rulerHeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                binding.currStepTV.setText(String.valueOf((int) value));
            }
        });

        binding.currStepTV.setText((TextUtils.isEmpty(ud.getDreamStepNumber()) || "0".equals(ud.getDreamStepNumber())) ? "5000" : ud.getDreamStepNumber());
        binding.rulerHeight.setValue((TextUtils.isEmpty(ud.getDreamStepNumber()) || "0".equals(ud.getDreamStepNumber())) ? 5000 : Integer.parseInt(ud.getDreamStepNumber()), 2000, 30000, 100);

        binding.nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.dream(ud.getDreamWeight(), ud.getDreamWeightPeriod(), binding.currStepTV.getText().toString());
            }
        });

        setRx(AppConstans.DREAM, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(TargetStepActivity.this);
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.dismiss(TargetStepActivity.this);
                ud.setDreamWeight(ud.getDreamWeight());
                ud.setDreamWeightPeriod(ud.getDreamWeightPeriod());
                ud.setDreamStepNumber(binding.currStepTV.getText().toString());
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(ud, UserInfoRes.DataBean.class));
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(TargetStepActivity.this, error.getMessage());
                DialogUtils.dismiss(TargetStepActivity.this);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_target_step;
    }

    @Override
    protected Class getViewModelClass() {
        return TargetViewModel.class;
    }
}
